package com.varian.system.listener;

import com.rabbitmq.client.Channel;
import com.varian.system.constant.SystemConstant;
import com.varian.system.dto.UserLoginDTO;
import com.varian.system.service.ISysUserService;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

/**
 * @author ben
 * @since 2024/8/12
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class UserLoginListener {

    private final ISysUserService userService;

    @SneakyThrows
    @RabbitListener(queues = SystemConstant.QUEUE_USER_LOGIN_LOG)
    public void onMessage(UserLoginDTO message, MessageProperties properties, Channel channel) {
        try {
            userService.updateUserLoginInfo(message);
        } catch (Exception e) {
            log.error("更新用户登录信息失败", e);
            channel.basicReject(properties.getDeliveryTag(), true);
        }
    }
}
